import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:lwtools/untils/unitl.dart';

class DeleteApkPage extends StatefulWidget {
  const DeleteApkPage({Key? key}) : super(key: key);

  @override
  State<DeleteApkPage> createState() => _DeleteApkPageState();
}

class _DeleteApkPageState extends State<DeleteApkPage> {
  TextStyle leftStyle = const TextStyle(
    color: Colors.black87,
    fontSize: 16,
  );
  List<String> _branchNameList = [];
  String _currentBranchName = '';
  int _currentBranchValue = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getBranchNames();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        margin: EdgeInsets.all(10),
        child: Column(
          children: [
            Row(
              children: [
                Text(
                  '选择分支:',
                  style: leftStyle,
                ),
                const SizedBox(
                  width: 20,
                ),
                DropdownButton(
                  items: _buildMenuItems(),
                  onChanged: <int>(value) {
                    setState(() {
                      _currentBranchValue = value;
                    });
                  },
                  icon: const Icon(Icons.arrow_drop_down),
                  value: _currentBranchValue,
                  focusColor: Colors.transparent,
                  // dropdownColor: Colors.transparent,
                ),
              ],
            ),
            Container(
              padding: EdgeInsets.symmetric(vertical: 10),
              child: Row(
                children: [
                  Text(
                    '自定义筛选条件:',
                    style: leftStyle,
                  ),
                  const SizedBox(
                    width: 20,
                  ),
                  Container(
                    margin: const EdgeInsets.only(left: 10),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5), border: Border.all(width: 1, color: Colors.grey)),
                    alignment: Alignment.center,
                    padding: const EdgeInsets.symmetric(vertical: 3, horizontal: 10),
                    child: _buildTF(
                      '自定义筛选条件',
                      (value) {},
                    ),
                    width: 380,
                    height: 30,
                  )
                ],
              ),
            ),
            GestureDetector(
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.blue,
                    borderRadius: BorderRadius.circular(5),
                    border: Border.all(width: 1, color: Colors.grey)),
                padding: const EdgeInsets.symmetric(horizontal: 100, vertical: 10),
                margin: EdgeInsets.only(top: 50),
                child: Text(
                  '开始删除',
                  style: leftStyle,
                ),
              ),
              onTap: () {
                LWUnitls.execute('./delete_apk.sh *.dart', (data) {
                  print("lw=====>$data");
                });
              },
            ),
            GestureDetector(
              child: Container(
                child: Text('哈哈哈'),
              ),
              onTap: () {
                // _getBranchNames();
              },
            )
          ],
        ),
      ),
    );
  }

  _buildMenuItems() {
    List<DropdownMenuItem<int>> list = [];
    for (int i = 0; i < _branchNameList.length; i++) {
      if (_currentBranchName == _branchNameList[i]) {
        _currentBranchValue = i;
      }
      list.add(DropdownMenuItem<int>(
        child: Text(_branchNameList[i]),
        value: i,
      ));
    }
    return list;
  }

  _getBranchNames() {
    String command = 'git branch';
    _branchNameList = [];
    LWUnitls.execute(command, (data) {
      if (data is String) {
        data.split('\n').forEach((element) {
          element = element.replaceAll('*', '');
          element = element.replaceAll(' ', '');
          if (element.isNotEmpty) {
            setState(() {
              _branchNameList.add(element);
            });
          }
        });
      }
    });
    String command1 = 'git rev-parse --abbrev-ref HEAD';
    LWUnitls.execute(command1, (data) {
      setState(() {
        _currentBranchName = data;
      });
    });
  }

  _buildTF(String? hitText, Function onChanged, {String defaultValue = ''}) {
    TextEditingController editingController = TextEditingController(text: defaultValue);
    return TextField(
      controller: editingController,
      decoration: InputDecoration(
          isCollapsed: true, border: InputBorder.none, hintText: hitText, hintStyle: const TextStyle(fontSize: 10)),
      onChanged: (value) {
        setState(() {
          // if (value.contains(' ')) {
          //   value = value.replaceAll(' ', '');
          // }
          onChanged(value);
        });
      },
    );
  }
}
